<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" lay-filter="plugin-info">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">id:</label>
        <div class="layui-input-block">
            <input type="text" name="id" placeholder="请填写入插件id" class="layui-input"
                   required disabled/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">stars:</label>
        <div class="layui-input-block">
            <textarea name="stars" placeholder="插件配置信息"
                      lay-verify="required" lay-reqtext="stars不得为空"
                      class="layui-textarea">{}</textarea>
            <tip>stars，JSON数组格式，为空请填写[]！</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script id="categoies_tpl" type="text/html">
    <select lay-verify="required" name="category_id">
        <option value="0">默认组</option>
        {{# layui.each(d, function(index, item){ }}
        <option value="{{ item.id }}">{{ item.title }}</option>
        {{# }); }}
    </select>
</script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/api.js?v=1.0.0" charset="utf-8"></script>
<script src="../../js/common.js?v=1.0.0" charset="utf-8"></script>
<script>
    const init = ()=>{
        layer.load(1)
        let tasks = []
        tasks.push(
            user_get(getQueryString('id')).then(res => {
                if (res.status === 'ok') {
                    res.data.stars = JSON.stringify(res.data.stars, null, 4)
                    layui.form.val('plugin-info', res.data)
                }
            }))
        Promise.all(tasks).finally(() => {
            layer.closeAll('loading')
        })
    }
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer;
        init()
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            layer.load(1)
            try {
                data.field.stars = JSON.parse(data.field.stars)
            } catch (e) {
                $message.error('stars不是合法的JSON数据格式，请重试');
                layer.closeAll('loading')
                return false
            }
            user_update(data.field).then(res => {
                if (res.status === 'ok') {
                    $message.success('保存成功');
                    init()
                }
            }).finally(() => {
                layer.closeAll('loading')
            })
            return false;
        });

    });
</script>
</body>
</html>